﻿using CoachingOrganizer.ClientInfrastructure.ServicesEntities.LocalStorage.Tools.MoneyCalculation;
using CoachingOrganizer.Database.DataAccess;
using CoachingOrganizer.Toolkit.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoachingOrganizer.Database.DataAccessLayer.Tools.MoneyCalculation
{
    public class StableMoneyFlowItemDAL : IStableMoneyFlowItemInfo
    {
        private readonly DbManagerBase _dbManager;
        private StableMoneyFlowItemDA _da;
        private string _name;
        private decimal _amount;
        private TimeSpan _period;
        public DateTime _start;
        public DateTime _finish;
        private int _sign;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public decimal Amount
        {
            get { return _amount; }
            set { _amount = value; }
        }

        public TimeSpan Period
        {
            get { return _period; }
            set { _period = value; }
        }

        public DateTime Start
        {
            get { return _start; }
            set { _start = value; }
        }

        public DateTime Finish
        {
            get { return _finish; }
            set { _finish = value; }
        }

        public int Sign
        {
            get { return _sign; }
            set { _sign = value; }
        }

        public StableMoneyFlowItemDAL(DbManagerBase dbManager)
        {
            _dbManager = dbManager;
        }

        public StableMoneyFlowItemDA CreateNewDA(MoneyCalculationDA moneyCalculation)
        {
            return _da = new StableMoneyFlowItemDA
            {
                MoneyCalculationId = moneyCalculation.Id
            };
        }

        public ActionResult Save()
        {
            _da.Name = _name;
            _da.Amount = _amount;
            _da.PeriodDays = _period.TotalDays;
            _da.Start = _start;
            _da.Finish = _finish;
            _da.Sign = _sign;
            return _dbManager.SafeRun(() => _dbManager.Context.InsertOrReplace(_da));
        }

        public bool Load(int id)
        {
            StableMoneyFlowItemDA stableMoneyFlowItem = _dbManager.Context.Find<StableMoneyFlowItemDA>(da => da.Id == id);
            if (stableMoneyFlowItem == null)
                return false;
            Load(stableMoneyFlowItem);
            return true;
        }

        public void Load(StableMoneyFlowItemDA stableMoneyFlowItem)
        {
            _da = stableMoneyFlowItem;
            _name = _da.Name;
            _amount = _da.Amount;
            _sign = _da.Sign;
            _period = TimeSpan.FromDays(_da.PeriodDays);
            _start = _da.Start;
            _finish = _da.Finish;
        }

        public ActionResult Remove()
        {
            if (_da != null)
                return _dbManager.SafeRun(() => _dbManager.Context.Delete<StableMoneyFlowItemDA>(_da.Id));
            return ActionResult.GetErrorResult(new KeyNotFoundException());
        }
    }
}
